<?php

namespace app\service\controller;

use app\common\controller\ServiceOauth;
use external\SFTools;

class User extends ServiceOauth {
    public function initialize() {
        parent::initialize();
    }

    /**
     * 获取用户授权列表
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getUserAuthList() {
        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->getUserAuthListBuyUserFind($this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 获取用户授权列表 (带主、子账号权限标识)
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getUserAuthListToHas() {
        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->getUserAuthListToHasBuyUserFind($this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 获取用户授权列表 (调用者必须为主账号)
     * @param   string  fccode			经销商编码
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getSettingUserAuthList() {
        $fccode = htmlspecialchars(input("post.fccode"));
        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->getSettingUserAuthListBuyFccode($fccode, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 获取用户授权列表
     * @param   string  fccode			经销商编码
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function bingUser() {
        $fccode = htmlspecialchars(input("post.fccode"));
        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->bingUserAuth($fccode, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 新增用户授权
     * @param   string  fccode			经销商编码
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function addUserAuth() {
        $fccode = htmlspecialchars(input("post.fccode"));
        $pow_code = htmlspecialchars(input("post.pow_code"));

        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->addUserAuth($fccode, $this->userFind , $pow_code);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 切换用户授权
     * @param   string  fccode			经销商编码
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function selectUserAuth(){
        $fccode = htmlspecialchars(input("post.fccode"));
        $t = htmlspecialchars(input("get.t"));

        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->selectUserAuth($fccode, $this->userFind , $t);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 删除用户授权
     * @param   string  fccode			经销商编码
     * @param   string  user_uuid		用户唯一标识
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function delUserAuth(){
        $fccode = htmlspecialchars(input("post.fccode"));
        $user_uuid = htmlspecialchars(input("post.user_uuid"));
        $t = htmlspecialchars(input("get.t"));

        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->delUserAuth($fccode, $user_uuid, $this->userFind , $t);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 修改授权等级
     * @param   string  fccode			经销商编码
     * @param   string  user_uuid		用户唯一标识
     * @param   int     val     		状态值
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function setIsPrivate(){
        $fccode = htmlspecialchars(input("post.fccode"));
        $user_uuid = htmlspecialchars(input("post.user_uuid"));
        $val = htmlspecialchars(input("post.val"));

        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->updateUserAuthToIsPrivate($fccode, $user_uuid, $val, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 申请授权码
     * @param   string  fccode			经销商编码
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function getAppleUserAuth(){
        $fccode = htmlspecialchars(input("post.fccode"));

        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->getAppleCodeForUserAuth($fccode, $this->userFind);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }

    /**
     * 申请授权码
     * @param   string  fccode			经销商编码
     * @return  json                    Json对象 ( status / code / msg / data )
     */
    public function saveUserAuthLevel(){
        $fccode = htmlspecialchars(input("post.fccode"));
        $pow_code = htmlspecialchars(input("post.pow_code"));

        $userAuthLogic = new \backend\logic\UserAuthLogic;
        $result = $userAuthLogic->saveUserAuthLevel($fccode, $this->userFind , $pow_code);
        if (empty($result['error_code'])) {
            return SFTools::pullJson(1, NULL, $result);
        }
        else {
            return SFTools::pullJson(-1, $result['error_code']);
        }
    }
}
